package cn.taketoday.live.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

import cn.taketoday.live.WebSocketMessage;
import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.data.AutoReply;
import cn.taketoday.live.data.Barrage;
import cn.taketoday.live.data.HighLevelBarrage;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.tools.ParseIndentityTools;
import cn.taketoday.live.utils.JodaTimeUtils;

/**
 * @author TODAY 2021/5/17 22:34
 */
public class BarrageCommandHandler implements CommandHandler {

  @Override
  public void handle(CommandContext context) throws IOException {
    final JSONObject original = context.getOriginal();
    JSONArray array = original.getJSONArray("info");
    Barrage barrage = Barrage.getBarrage(((JSONArray) array.get(2)).getLong(0),
                                         ((JSONArray) array.get(2)).getString(1), array.getString(1),
                                         ((JSONArray) array.get(0)).getShort(9), ((JSONArray) array.get(0)).getLong(4),
                                         ((JSONArray) array.get(2)).getShort(2), ((JSONArray) array.get(2)).getShort(3),
                                         ((JSONArray) array.get(2)).getShort(4), ((JSONArray) array.get(2)).getInteger(5),
                                         ((JSONArray) array.get(2)).getShort(6),
                                         ((JSONArray) array.get(3)).size() <= 0 ? 0 : ((JSONArray) array.get(3)).getShort(0),
                                         ((JSONArray) array.get(3)).size() <= 0 ? "" : ((JSONArray) array.get(3)).getString(1),
                                         ((JSONArray) array.get(3)).size() <= 0 ? "" : ((JSONArray) array.get(3)).getString(2),
                                         ((JSONArray) array.get(3)).size() <= 0 ? 0L : ((JSONArray) array.get(3)).getLong(3),
                                         ((JSONArray) array.get(4)).getShort(0), ((JSONArray) array.get(4)).getString(3),
                                         ((JSONArray) array.get(5)).getString(0), ((JSONArray) array.get(5)).getString(1),
                                         array.getShort(7));
    final StringBuilder stringBuilder = new StringBuilder(200);
    // 过滤礼物自动弹幕
    if (barrage.getMsg_type() == 0) {
      HighLevelBarrage hbarrage = HighLevelBarrage.copyHbarrage(barrage);
      if (barrage.getUid().equals(GlobalData.AU_ID)) {
        hbarrage.setManager((short) 2);
      }
      // 判断类型输出
      stringBuilder.append(JodaTimeUtils.formatDateTime(barrage.getTimestamp()));
      stringBuilder.append(":收到弹幕:");
      if (context.contains(ShieldMessage.is_barrage_vip)) {
        // 老爷
        stringBuilder.append(ParseIndentityTools.parseVip(barrage));
      }
      else {
        hbarrage.setVip((short) 0);
        hbarrage.setSvip((short) 0);
      }
      if (context.contains(ShieldMessage.is_barrage_guard)) {
        // 舰长
        stringBuilder.append(ParseIndentityTools.parseGuard(barrage.getUguard()));
      }
      else {
        hbarrage.setUguard((short) 0);
      }
      if (context.contains(ShieldMessage.is_barrage_manager)) {
        // 房管
        stringBuilder.append(ParseIndentityTools.parseManager(barrage.getUid(), barrage.getManager()));
      }
      else {
        hbarrage.setManager((short) 0);
      }
      if (context.contains(ShieldMessage.is_barrage_medal)) {
        // 勋章+勋章等级
        if (!StringUtils.isEmpty(barrage.getMedal_name())) {
          stringBuilder.append("[").append(barrage.getMedal_name()).append(" ")
                  .append(barrage.getMedal_level()).append("]");
        }
      }
      else {
        hbarrage.setMedal_level(null);
        hbarrage.setMedal_name(null);
        hbarrage.setMedal_room(null);
        hbarrage.setMedal_anchor(null);
      }
      if (context.contains(ShieldMessage.is_barrage_ul)) {
        // ul等级
        stringBuilder.append("[").append("UL").append(barrage.getUlevel()).append("]");
      }
      else {
        hbarrage.setUlevel(null);
      }
      stringBuilder.append(barrage.getUname());
      stringBuilder.append(" TA说:");
      stringBuilder.append(barrage.getMsg());
      //控制台打印
      if (context.contains(ShieldMessage.is_cmd)) {
        System.out.println(stringBuilder.toString());
      }
      context.sendMessage(WebSocketMessage.of("danmu", hbarrage));
      //高级显示处理
//						try {
//							danmuWebsocket.sendMessage(WsPackage.toJson("danmu", (short)0, barrage));
//						} catch (Exception e) {
//							// TODO 自动生成的 catch 块
//							e.printStackTrace();
//						}
      //日志处理
      if (GlobalData.logThread != null && !GlobalData.logThread.FLAG) {
        GlobalData.logString.add(stringBuilder.toString());
        synchronized(GlobalData.logThread) {
          GlobalData.logThread.notify();
        }
      }
      //自动回复姬处理
      BarrageHandler.autoReply(AutoReply.of(barrage.getUid(), barrage.getUname(), barrage.getMsg()));
      stringBuilder.delete(0, stringBuilder.length());
    }
    else {
//				    LOGGER.debug("test中貌似为礼物弹幕：" + message);
    }
  }

}
